package com.bonorsoft.system.schedules;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bonorsoft.system.entity.SysMoney;
import com.bonorsoft.system.service.SysMoneyService;
import com.bonorsoft.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class WilliamSyncJob {

    @Autowired
    private SysMoneyService moneyService;

//    @Scheduled(cron = "0 0/1 * * * ?")
    /*
    定时任务，10分钟支付未完成的订单自动库存加一
     */
    public void job(){
        log.info("=========定时任务，10分钟支付未完成的订单自动库存加一=========");
        QueryWrapper<SysMoney> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_status", 0);
        List<SysMoney> list = moneyService.list(queryWrapper);
        for (SysMoney money:
                list) {
            Date now = new Date();
            Date date = money.getDate();
            Calendar calnow = Calendar.getInstance();
            calnow.setTime(now);
            Calendar caldate = Calendar.getInstance();
            caldate.setTime(date);
            int m = DateUtils.dateDiff('m', calnow, caldate);
            if(m > 10){
                moneyService.cancelOrder(money.getOpenid(),money.getOutTradeNo());
            }
        }
    }

    @Scheduled(cron = "0/1 * * * * ?")
    /*
    定时任务，10分钟支付未完成的订单自动库存加一
     */
    public void schel(){
        log.info("=========定时任务，每秒钟同步库存=========");
        QueryWrapper<SysMoney> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_status", 1);
        int count = moneyService.count(queryWrapper);
        Integer stroge = moneyService.getStroge();
        Integer sheng = 417 - count;
        if(sheng != stroge){
            moneyService.updateStroge(sheng);
        }
    }
}
